package com.company.test;

import java.util.Arrays;

//增删改查
//size capacity
//size ->
//data
//size
//capacity
//
public class Array<E> {
    private E[] data;
    private int size;
    private static int defaultCapaticy = 10;

    /**
     * @param capacity 构造函数  传入数组的容量 capacity构造Array
     */
    public Array(int capacity) {
        data = (E[]) new Object[capacity];
        size = 0;
    }

    public Array() {
        this(defaultCapaticy);
    }

    public int getSize() {
        return size;
    }

    public int getCapacity() {
        return data.length;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void addLast(E e) {
        add(size, e);
    }

    public void addFirst(E e) {
        add(0, e);
    }

    //在第index个位置插入一个新元素e
    public void add(int index, E e) {
        if (size == data.length)
            throw new IllegalArgumentException("AddLast failed. Array is full.");
        if (index < 0 || index > size)
            throw new IllegalArgumentException("AddLast failed. Required index>=0 and Array is full.");
        for (int i = size - 1; i >= index; i--)
            data[i + 1] = data[i];
        data[index] = e;
        size++;
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append(String.format("Array: size=%d, capacity:%d\n", size, data.length));
        res.append('{');
        for (int i = 0; i < size; i++) {
            res.append(data[i]);
            if (i != size - 1)
                res.append(", ");
        }
        res.append('}');
        return res.toString();
    }

    E get(int index) {
        if (index < 0 || index >= size)
            throw new IllegalArgumentException("Get failed. Index is illegal.");
        return data[index];
    }

    void set(int index, E e) {
        if (index < 0 || index >= size)
            throw new IllegalArgumentException("Set failed. Index is illegal.");
        data[index] = e;
    }

    //搜索 和删除元素
    //查找数组是否存在元素 e所在的位置索引位置
    public boolean contains(E e) {
        for (int i = 0; i < size; i++)
            if (data[i].equals(e))
                return true;
        return false;
    }

    //查找数组元素 e所在的索引 如果不存在则-1
    public int find(E e) {
        for (int i = 0; i < size; i++)
            if (data[i].equals(e))
                return i;
        return -1;
    }

    //指定位置删除
    //删除索引为1的元素
    //将索引2移动到1的位置
    //一次赋值的过程
    //data[1]=data[2]
    //将后面的元素往前面移动即可
    //添加是往后移动
    //删除是往前一点
    //size--
    //size++
    //第一个size位置
    //size--
    //指向100即可
    //0-size
    //删除成功 删除元素是什么？remove 返回删除的元素
    public E remove(int index) {
        if (index < 0 || index > size)
            throw new IllegalArgumentException("Remove failed. Index is illegal.");
        E ret = data[index];
        for (int i = index + 1; i < size; i++)
            data[i - 1] = data[i];
        size--;
        data[size]= null;//loitering objects !=memory leak
        return ret;
    }

    public E removeFirst() {
        return remove(0);
    }

    public E removeLast() {
        return remove(size - 1);
    }

    public void removeElement(E e) {
        int index = find(e);
        if (index != -1)
            remove(index);
    }
}
